/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.market;

import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.dto.ShopOffer;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MarketManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MarketManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class SellPage extends WebPage {

    private MarketManager marketManager = new MarketManagerImpl();
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private String selectedCity = "";
    private String wanted = "Axe";
    private String offered = "Axe";
    private List<String> cities;

    public SellPage(PageParameters parameters) {

        List<String> RESOURCE = new ArrayList<>();

        for (ResourceType r : ResourceType.values()) {
            RESOURCE.add(r.toString());
        }

        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("shopOfferId")) {
            marketManager.cancelShopOffer(parameters.getLong("shopOfferId"));
            info("Shop offer has been deleted");
        }
        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));


            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }
            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            p.add("cityName", selectedCity);

            add(new MarketPanel("marketPanel", p));

            final DropDownChoice<String> from = new DropDownChoice<>("from",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            final DropDownChoice<String> wantedResource = new DropDownChoice<>("wantedResource",
                    new PropertyModel<String>(this, "wanted"), RESOURCE);

            final RequiredTextField<String> wantedCount = new RequiredTextField<>("wantedCount", Model.of(""));
            wantedCount.add(new CountValidator());

            final DropDownChoice<String> offeredResource = new DropDownChoice<>("offeredResource",
                    new PropertyModel<String>(this, "offered"), RESOURCE);

            final RequiredTextField<String> offeredCount = new RequiredTextField<>("offeredCount", Model.of(""));
            offeredCount.add(new CountValidator());

            Form<?> form = new Form<Void>("form") {
                @Override
                protected void onSubmit() {
                    selectedCity = cities.get(Integer.parseInt(from.getValue()));
                    PageParameters pageParameters = new PageParameters();

                    if (cityResourcesManager.getResourceCount(cityManager.getCityByName(selectedCity).getId(),
                            ResourceType.valueOf(offered)) > Integer.parseInt(offeredCount.getValue())) {

                        ShopOffer shopOffer = new ShopOffer();
                        shopOffer.setPlayerId(cityManager.getOwnerOfCity(cityManager.getCityByName(selectedCity).getId()).getId());
                        shopOffer.setCityId(cityManager.getCityByName(selectedCity).getId());

                        shopOffer.setWantedResource(ResourceType.valueOf(wanted));
                        shopOffer.setWantedResourceCount(Integer.parseInt(wantedCount.getValue()));

                        shopOffer.setSoldResource(ResourceType.valueOf(offered));
                        shopOffer.setSoldResourceCount(Integer.parseInt(offeredCount.getValue()));

                        marketManager.createShopOffer(shopOffer);
                        pageParameters.add("id", id.toString());
                        pageParameters.add("cityName", selectedCity);
                        setResponsePage(SellPage.class, pageParameters);
                    } else {
                        error("You do not have enough to make an offer.");
                    }
                }
            };

            add(form);
            form.add(from);
            form.add(wantedResource);
            form.add(wantedCount);
            form.add(offeredResource);
            form.add(offeredCount);

            List<ShopOffer> list = marketManager.getShopOffersOfPlayer(id);

            ListView listview = new ListView("listview", list) {
                @Override
                protected void populateItem(ListItem item) {
                    ShopOffer shopOffer = (ShopOffer) item.getModelObject();
                    item.add(new Label("wanted", shopOffer.getWantedResource().toString() + ": " + shopOffer.getWantedResourceCount()));
                    item.add(new Label("offered", shopOffer.getSoldResource().toString() + ": " + shopOffer.getSoldResourceCount()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("shopOfferId", shopOffer.getId().toString());
                    pageParameters.add("type", "recieved");
                    BookmarkablePageLink<Void> link = new BookmarkablePageLink<>("delete", SellPage.class, pageParameters);
                    link.add(new Label("delete", "Remove"));
                    item.add(link);
                }
            };
            add(listview);
        }
    }
}
